package com.cssnj.portal.web.user;


import com.cssnj.portal.service.user.FavoriteService;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.domain.exception.BaseException;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping(value = "/user/favorite")
public class FavoriteController {

    @Autowired
    FavoriteService favoriteService;

    @Autowired
    private Environment environment;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping(value = {"/index.do"})
    public ModelAndView index(HttpServletRequest request) throws BaseException, SQLException {

        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.setHandleCode("getFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);

        return new ModelAndView("user/favorite").addObject("scjlist",jsonResponse.getRepData().get("scjlist"));

    }

    @ResponseBody
    @RequestMapping(value = {"/removeFolder.do"})
    public JsonResponse removeFolder(HttpServletRequest request, String scj_id) throws BaseException, SQLException {

        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("scj_id",scj_id);
        jsonRequest.setHandleCode("removeFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);
        return jsonResponse;
    }

    @ResponseBody
    @RequestMapping(value = {"/addFolder.do"})
    public JsonResponse addFolder(HttpServletRequest request, String name) throws BaseException, SQLException {

        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("name",name);
        jsonRequest.setHandleCode("addFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);
        return jsonResponse;
    }

    @ResponseBody
    @RequestMapping(value = {"/renameFolder.do"})
    public JsonResponse renameFolder(HttpServletRequest request, String scj_id,String name) throws BaseException, SQLException {

        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("scj_id",scj_id);
        jsonRequest.getReqData().put("name",name);
        jsonRequest.setHandleCode("renameFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);
         return jsonResponse;
    }

    @ResponseBody
    @RequestMapping(value = {"/topFolder.do"})
    public JsonResponse topFolder(HttpServletRequest request, String scj_id,String name) throws BaseException, SQLException {

        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("scj_id",scj_id);
        jsonRequest.getReqData().put("name",name);
        jsonRequest.setHandleCode("topFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);

        return jsonResponse;
    }

    @ResponseBody
    @RequestMapping(value = {"/searchGn.do"})
    public JsonResponse searchGn(HttpServletRequest request,String scj_id) throws BaseException, SQLException {
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("scj_id",scj_id);
        jsonRequest.setHandleCode("getGnByFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);
        return jsonResponse;
    }

    @ResponseBody
    @RequestMapping(value = {"/removeGn.do"})
    public JsonResponse removeGn(HttpServletRequest request,String scj_id,String gn_id) throws BaseException, SQLException {

        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("scj_id",scj_id);
        jsonRequest.getReqData().put("gn_id",gn_id);
        jsonRequest.setHandleCode("removeGnFromFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);
         return jsonResponse;
    }

    @RequestMapping(value = {"/toAddGn.do"})
    public ModelAndView toAddGn(HttpServletRequest request) throws BaseException, SQLException {
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.setHandleCode("getFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);
        return new ModelAndView("user/favoriteAdd").addObject("scjlist",jsonResponse.getRepData().get("scjlist"));
    }

    @ResponseBody
    @RequestMapping(value = {"/addGn.do"})
    public JsonResponse addGn(HttpServletRequest request,String scj_id,String gn_id) throws BaseException, SQLException {
        JsonRequest jsonRequest = new JsonRequest();
        jsonRequest.getReqData().put(environment.getProperty(SysConstants.AUTH_SESSION_NAME), SecurityUtils.getSubject().getPrincipal());
        jsonRequest.getReqData().put("scj_id",scj_id);
        jsonRequest.getReqData().put("gn_id",gn_id);
        jsonRequest.setHandleCode("addGnToFolder");
        JsonResponse jsonResponse = favoriteService.execute(jsonRequest);
        return jsonResponse;
    }


}
